﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using WinHBM.Core.Net.Http;
using System.Windows.Forms;
using WinHBM.Core.Bluetooth;
using InTheHand.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Net.NetworkInformation;
namespace WinHBM.Core.Net.Sockets.Cmd
{
    public class NetWorkConnectCmd : SocketCmd
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
        //public static bool IsConnectInternet()
        //{
        //    int Description = 0;
        //    return InternetGetConnectedState(Description, 0);
        //}
        private HttpClient httpClient = HttpClient.Instance;
        public override void executeCmd(JObject request, FlashSocketServer socket)
        {
            
            Ping ping = new Ping();
            try
            {
                PingReply pingReply = ping.Send("www.baidu.com");
                if (pingReply.Status == IPStatus.Success)
                {

                    Console.WriteLine("当前在线，已ping通！");
                    socket.Send(wrapCmd(CMD_NETWORKCONNECT, "true"));
                }
                else
                {
                    Console.WriteLine("不在线，ping不通！");
                    socket.Send(wrapCmd(CMD_NETWORKCONNECT, "false"));
                    Thread.Sleep(3000);
                }
            }
            catch (Exception)
            {
                socket.Send(wrapCmd(CMD_NETWORKCONNECT, "false"));
                Thread.Sleep(3000);
            }
           
            //    if (IsConnectInternet())
            //{
            //    Console.WriteLine("网络通畅"); 
            //}
            //else
            //{
            //    Console.WriteLine("网络不通");
            //}
            
            
        }
    }
}
